﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace ERModelData
{
    class Markers
    {
        /// <summary>[Key] Маркеры скрыты, доступно только для чтения</summary>
        public bool IsHiden { get; private set; }

        /// <summary>[Key] Маркеры показанны, доступно только для чтения</summary>
        public bool IsShow { get; private set; }

        /// <summary>Обновление Z индекса</summary>
        /// <param name="zIndex">Начальный индекс</param>
        /// <returns>Конечный индекс</returns>
        public int UpdateZIndexMarkers(int zIndex)
        {
            Canvas.SetZIndex(TopLeft, zIndex++);
            Canvas.SetZIndex(Top, zIndex++);
            Canvas.SetZIndex(TopRight, zIndex++);
            Canvas.SetZIndex(Right, zIndex++);
            Canvas.SetZIndex(BottomRight, zIndex++);
            Canvas.SetZIndex(Bottom, zIndex++);
            Canvas.SetZIndex(BottomLeft, zIndex++);
            Canvas.SetZIndex(Left, zIndex);
            return zIndex;
        }
        
        /// <summary>Отображение маркеров</summary>
        /// <param name="essence">Cущность</param>
        protected void ShowMarkers()
        {
            UpdatePositionMarkers();
            
            if (IsHiden)
            {
                canvas.Children.Add(TopLeft);
                canvas.Children.Add(Top);
                canvas.Children.Add(TopRight);
                canvas.Children.Add(Right);
                canvas.Children.Add(BottomRight);
                canvas.Children.Add(Bottom);
                canvas.Children.Add(BottomLeft);
                canvas.Children.Add(Left);
            }

            IsHiden = false;
            IsShow = true;
        }

        /// <summary>Сокрытие маркеров трансформации сущности</summary>
        protected void HidenMarkers()
        {
            if (IsShow)
            {
                canvas.Children.Remove(TopLeft);
                canvas.Children.Remove(Top);
                canvas.Children.Remove(TopRight);
                canvas.Children.Remove(Right);
                canvas.Children.Remove(BottomRight);
                canvas.Children.Remove(Bottom);
                canvas.Children.Remove(BottomLeft);
                canvas.Children.Remove(Left);
            }

            IsHiden = true;
            IsShow = false;
        }
        
        /// <summary>Маркер верхнего левого угла</summary>
        protected Shape TopLeft;
        /// <summary>Маркер верхней границы</summary>
        protected Shape Top;
        /// <summary>Маркер верхнего правого угла</summary>
        protected Shape TopRight;
        /// <summary>Маркер правой границы</summary>
        protected Shape Right;
        /// <summary>Маркер нижнего правого угла</summary>
        protected Shape BottomRight;
        /// <summary>Маркер нижней границы</summary>
        protected Shape Bottom;
        /// <summary>Маркер нижнего левого угла</summary>
        protected Shape BottomLeft;
        /// <summary>Маркер левой границы</summary>
        protected Shape Left;

        /// <summary>Холст на котором ведется отрисовка</summary>
        protected Canvas canvas;

        /// <summary>Cущность</summary>
        protected Essence essence;

        /// <summary>Обновление позиций маркеров</summary>
        protected void UpdatePositionMarkers()
        {
            Canvas.SetTop(TopLeft, Canvas.GetTop(essence) - Top.Width / 2);
            Canvas.SetLeft(TopLeft, Canvas.GetLeft(essence) - Top.Width / 2);

            Canvas.SetTop(Top, Canvas.GetTop(essence) - Top.Width / 2);
            Canvas.SetLeft(Top, Canvas.GetLeft(essence) + essence.Width / 2 - Top.Width / 2);

            Canvas.SetTop(TopRight, Canvas.GetTop(essence) - Top.Width / 2);
            Canvas.SetLeft(TopRight, Canvas.GetLeft(essence) + essence.Width - Top.Width / 2);

            Canvas.SetTop(Right, Canvas.GetTop(essence) + essence.Height / 2 - Top.Width / 2);
            Canvas.SetLeft(Right, Canvas.GetLeft(essence) + essence.Width - Top.Width / 2);

            Canvas.SetTop(BottomRight, Canvas.GetTop(essence) + essence.Height - Top.Width / 2);
            Canvas.SetLeft(BottomRight, Canvas.GetLeft(essence) + essence.Width - Top.Width / 2);

            Canvas.SetTop(Bottom, Canvas.GetTop(essence) + essence.Height - Top.Width / 2);
            Canvas.SetLeft(Bottom, Canvas.GetLeft(essence) + essence.Width / 2 - Top.Width / 2);

            Canvas.SetTop(BottomLeft, Canvas.GetTop(essence) + essence.Height - Top.Width / 2);
            Canvas.SetLeft(BottomLeft, Canvas.GetLeft(essence) - Top.Width / 2);

            Canvas.SetTop(Left, Canvas.GetTop(essence) + essence.Height / 2 - Top.Width / 2);
            Canvas.SetLeft(Left, Canvas.GetLeft(essence) - Top.Width / 2);
        }
    }
}
